# -*- coding: utf-8 -*-
"""
Created on Wed Dec 19 21:51:07 2012

@author: Winand
"""
if __name__ == '__main__': import sys; sys.path.append("C:\\WinPython-32bit-2.7.3.1\\projects\\lancomm")

from webapi.general import hasKeys
from webapi import config
from rutracker import RuTracker
from webapi import http
from webapi.log import error

name="Webpage Update Tracker"
description="Service monitors specified webpages and downloads file to a chosen folder on update"
path="tracker"

TRACKERS = (
    RuTracker,
)

#TRK_TOPIC="4214807"
#COOKIE_FILE="C:\\cookie_rutracker.txt"
#TOR_PATH_OUT="C:\\"
#
pages={}
#
#class Tracker():
#    def addWebpage(self, url):
#        if url in pages: return True #already added
#        for i in TRACKERS:
#            if i.canHandle(url):
#                j = i(TRK_USER, TRK_PASS, COOKIE_FILE)
#                j.authenticate()
#                pages[url] = [j.getTopicName(url), j.getTorrentTimestamp(url), RuTracker.__name__]
#                return True

def getPage(options, resp):
    http.templateResponse(resp, "tpl_svc_tracker_home.html", \
                        {'list_len': 3,
                         'topic': [i for i in ("4214807", "4200375", "4107464")],
                         'srv_name': path,
                        })
                        
def getAjaxChunk(options, resp):
    if len(options):
        if options[0].startswith("get_topic_name"):
            topic=options[0].lstrip("get_topic_name")
            rutr=RuTracker("","")
            http.templateResponse(resp, "tpl_svc_tracker_home.html", \
                                {'topicname': rutr.getTopicName("http://rutracker.org/forum/viewtopic.php?t="+topic),
                                }, ajaxblock="get_topic_name%topic%")
        else: error("unknown ajax request", options[0])
    else: error("empty ajax request")

def getConfPage(options, resp):
    pass
#    url_list = config.cget("tracker/url_list", {})
#    last_err = ""
#    if len(options):
#        if options[0] == "add" and hasKeys(resp.post_query, 'url'):
#            new_url=resp.post_query['url'].value
#            for handler in TRACKERS:
#                if handler.canHandle(new_url):
#                    pass
#
#                    
#        elif options[0] == "del" and hasKeys(resp.query, 'mac'):
#            mac_s = resp.query['mac'].value
#            mac = __parseDispMacAddr(mac_s)
#            if mac in mac_list:
#                del mac_list[mac]
#                config.cset("wake/mac_list", mac_list)
#            else: last_err = "Cannot delete " + mac_s + ". Not found"
#    mac_list_disp = map(__dispMacAddr, mac_list.keys())
#            
#    arpc = __getArpCache()
#    http.templateResponse(resp, "tpl_svc_wake_conf.html", \
#                        {'brd_addr': getBroadcastAddress(),
#                        'root_path': SRV_CONF_ROOT_PATH+"/"+path,
#                        'arp_names': arpc[0],
#                        'arp_macs': arpc[2],
#                        'arp_len': len(arpc[0]),
#                        'mac_list_len': len(mac_list),
#                        'mac_list': mac_list_disp,
#                        'mac_list_desc': mac_list.values(),
#                        'lasterror': last_err,
#                        })
                
#if __name__ == '__main__':
#    Tracker().addWebpage("http://rutracker.org/forum/viewtopic.php?t=4214807")
#    serialize.save(pages, "c:\\json.txt")